package book.demo3;

import io.netty.bootstrap.Bootstrap;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.oio.OioSocketChannel;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class BootStrapDemo {

    public void demo1(){
        NioEventLoopGroup group = new NioEventLoopGroup();
        // 创建引导实例
        Bootstrap bootstrap = new Bootstrap();

        //绑定Channel和EventGroup
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .handler(new SimpleChannelInboundHandler<ByteBuf>() {
                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
                        System.out.println("Received data:"+msg.toString(StandardCharsets.UTF_8));
                    }
                });

        ChannelFuture future = bootstrap.connect(new InetSocketAddress("www.baidu.com",80));
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()){
                    System.out.println("Connection established");
                }else {
                    System.out.println("Connction attempt failed");
                    future.cause().printStackTrace();
                }
            }
        });
    }

    /**
     * Channel和EventLoopGroup的兼容性
     *
     * channel
     * ├───nio
     * │　　　　 NioEventLoopGroup
     * ├───oio
     * │　　　　 OioEventLoopGroup
     * └───socket
     * 　　 ├───nio
     * 　　 │　　　　 NioDatagramChannel
     * 　　 │　　　　 NioServerSocketChannel
     * 　　 │　　　　 NioSocketChannel
     * 　　 └───oio
     * 　　　　　　　 OioDatagramChannel
     * 　　　　　　　 OioServerSocketChannel
     * 　　　　　　　 OioSocketChannel
     */
    public void demo2(){
        NioEventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();

        bootstrap.group(group)
                .channel(OioSocketChannel.class)
                .handler(new SimpleChannelInboundHandler<ByteBuf>() {
                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
                        System.out.println("Receive data");
                    }
                });

        ChannelFuture future = bootstrap.connect(new InetSocketAddress("www.baidu.com", 80));
        future.syncUninterruptibly();

    }

    /**
     * Bootstra引导服务器
     */
    public void demo3(){
        NioEventLoopGroup group = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();

        bootstrap.group(group)
                .channel(NioServerSocketChannel.class)
                .childHandler(new SimpleChannelInboundHandler<ByteBuf>() {
                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
                        System.out.println("Recevied data"+msg.toString(StandardCharsets.UTF_8));
                    }
                });

        ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080));
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()){
                    System.out.println("Server bound");
                }else {
                    System.err.println("Bound attempt failed");
                    future.cause().printStackTrace();
                }
            }
        });
    }

    public static void main(String[] args) {
        new BootStrapDemo().demo2();
    }

}
